<template>
	<view class="content">
		<u-cell-group>
			<u-swipe-action v-for="item in visitlist" :key="item.id">
				<u-swipe-action-item :options="options2" @click="changeOperation" :name="item.id">
					<view class="swipe-action u-border-top u-border-bottom">
						<view class="swipe-action__content">
							<u-cell :title="item.content"
								:value="item.business.nickname" :label="item.createtime" center></u-cell>
						</view>
					</view>
				</u-swipe-action-item>
			</u-swipe-action>
		</u-cell-group>
		<u-button @click="add" :customStyle="btnStyle" color="#19BC9C" type="success">添加回访记录</u-button>
			<u-modal :show="show" :content='content' :showCancelButton="true" @confirm="confirm" @cancel="show = false"></u-modal>
			<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				btnStyle: {
					width: '100%',
					position: 'fixed',
					bottom: '0px'
				},
				visitlist: [],
				LoginUser: {},
				options2: [{
					text: '编辑',
					id: 'edit',
					style: {
						backgroundColor: '#3c9cff'
					}
				}, {
					text: '删除',
					id: 'del',
					style: {
						backgroundColor: '#f56c6c'
					}
				}],
				show: false,
				content: '确认是否删除当前回访记录?',
				id: 0
			}
		},
		onLoad() {
			this.LoginUser = uni.getStorageSync('LoginUser') ? uni.getStorageSync('LoginUser') : {};
			this.$u.auth.check()
			this.getvisitdata()
		},
		methods: {
			async getvisitdata() {
				let result = await this.$u.api.business.index({
					adminid: this.LoginUser.id
				})
				if (result.code == 1) {
					this.visitlist = result.data
				}
			},
			add() {
				uni.navigateTo({
					url: '/pages/business/visit/add'
				})
			},
			changeOperation(e){
				if(e.index == 0){
					uni.navigateTo({
						url: '/pages/business/visit/edit?id=' + e.name,
						
					})
				} else {
					this.id = e.name
					this.getdeldata()
				}
			},
			getdeldata(){
				this.show = true
			},
			async confirm(){
				this.show = false
				let result = await this.$u.api.business.del({id: this.id})
				if(result.code == 1){
					this.$refs.uToast.show({
						type: 'success',
						message: result.msg,
					})
					this.getvisitdata()
				}  else {
					this.$refs.uToast.show({
						type: 'error',
						message: result.msg
					})
				}
			}
		}
	}
</script>

<style>
	.content {
		height: 100vh;
	}
</style>
